package com.zmn.oms.services.impl.wallquotation;

import com.zmn.base.plat.engine.common.constant.PlatEngineConsts;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.oms.model.entity.wallquotation.WallQuotation;
import com.zmn.oms.model.entity.wallquotation.WallQuotationQuery;
import com.zmn.oms.persistence.interfaces.wallquotation.WallQuotationDao;
import com.zmn.oms.services.interfaces.wallquotation.WallQuotationService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * 描述: 墙面报价订单接口实现类
 *
 * @author chengguang
 * @since  2022/05/25 14:10
 */
@Slf4j
@Service
public class WallQuotationServiceImpl implements WallQuotationService {

    private static final String TAG = "墙面报价订单基础服务接口";

    @Resource
    private WallQuotationDao wallQuotationDao;

    @Override
    public WallQuotation findById(Long workId) {
        if (Objects.isNull(workId)) {
            log.error("[{}],根据id查询数据workId为null", TAG);
            return null;
        }
        return wallQuotationDao.findById(workId);
    }

    @Override
    public Integer findMaxSort() {
        return wallQuotationDao.findMaxSort();
    }

    @Override
    public List<WallQuotation> listByIds(List<Long> workIds) {
        if (CollectionUtils.isEmpty(workIds)) {
            log.error("[{}],按条件查询数据,workIds为null", TAG);
            return Collections.emptyList();
        }
        return wallQuotationDao.listByIds(workIds);
    }

    @Override
    public List<WallQuotation> listByQuery(WallQuotationQuery query) {
        if (Objects.isNull(query)) {
            log.error("[{}],按条件查询数据,query为null", TAG);
            return Collections.emptyList();
        }
        return wallQuotationDao.listByQuery(query);
    }

    @Override
    public List<WallQuotation> listPageByQuery(WallQuotationQuery query) {
        if (Objects.isNull(query)) {
            log.error("[{}],按条件查询数据,query为null", TAG);
            return Collections.emptyList();
        }
        Integer count = countByQuery(query);
        query.setTotalCount(count);
        if (Objects.equals(count, PlatEngineConsts.NONE)){
            return new ArrayList<>();
        }
        return wallQuotationDao.listPageByQuery(query);
    }

    @Override
    public Integer countByQuery(WallQuotationQuery query) {
        if (Objects.isNull(query)) {
            log.error("[{}],按条件统计数据,query为null", TAG);
            return null;
        }
        return wallQuotationDao.countByQuery(query);
    }

    @Override
    public Integer updateByKey(WallQuotation wallQuotation) {
        if (Objects.isNull(wallQuotation.getWorkId())){
            log.error("[{}],根据id修改数据,wallQuotation为null", TAG);
            return null;
        }
        return wallQuotationDao.updateByKey(wallQuotation);
    }

    @Override
    public void updateStatusBatch(List<Long> ids, Integer status, String operator) {
        if (!Objects.equals(status, PlatEngineConsts.NO) && !Objects.equals(status, PlatEngineConsts.YES)) {
            log.error("[{}],status状态超出取值范围", TAG);
            return;
        }
        wallQuotationDao.updateStatusBatch(ids, status, operator, DateUtil.getNow());
    }

    @Override
    public Integer insert(WallQuotation wallQuotation){
        if (Objects.isNull(wallQuotation)){
            log.error("[{}],新增数据,wallQuotation为null", TAG);
            return null;
        }
        return wallQuotationDao.insert(wallQuotation);
    }

    @Override
    public void insertBatch(List<WallQuotation> list){
        if (CollectionUtils.isEmpty(list)) {
            log.error("[{}],数据为空", TAG);
            return;
        }
        wallQuotationDao.insertBatch(list);
    }

    @Override
    public Integer deleteByKey(Long workId) {
        if (Objects.isNull(workId)){
            log.error("[{}],根据id删除数据,workId为null", TAG);
            return null;
        }
        return wallQuotationDao.deleteByKey(workId);
    }

}